package com.baiyun.service.impl;

import com.baiyun.domain.Menu;
import com.baiyun.domain.Role;
import com.baiyun.domain.UserDTO;
import com.baiyun.service.MenuService;
import com.baiyun.service.RoleService;
import com.baiyun.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baiyun.domain.User;
import com.baiyun.service.UserService;
import com.baiyun.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
* @author 20516
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2022-06-17 21:39:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    RoleService roleService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MenuService menuService;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public User getByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username", username));
    }

    /**
     * 获取角色、菜单
     * @param id
     * @return
     */
    @Override
    public String getUserAuthority(Long id) {

        User user = userMapper.selectById(id);

        String authority = "";
        //判断缓存是否有
        if (redisUtil.hasKey("GrantedAuthority:"+user.getUsername())){
            authority = (String) redisUtil.get("GrantedAuthority:" + user.getUsername());
        }else {
            //获取角色
            List<Role> roles = roleService.list(new QueryWrapper<Role>().inSql("id", "select role_id from sys_user_role where user_id = " + id));
            if (roles.size()>0){
                String collect = roles.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
                authority = collect.concat(",");
            }

            //获取菜单
            List<Long> menuIds = userMapper.getNavMenuIds(id);
            if (menuIds.size()>0){
                List<Menu> menus = menuService.listByIds(menuIds);
                String menuPerms = menus.stream().map(m -> m.getPerms()).collect(Collectors.joining(","));
                authority = authority.concat(menuPerms);
            }
            redisUtil.set("GrantedAuthority:"+user.getUsername(),authority,60*60);
        }
        return authority;
    }

    @Override
    public void clearUserAuthority(String username) {
        redisUtil.del("GrantedAuthority:"+username);
    }

    @Override
    public void clearUserAuthorityByRoleId(Long roleId) {
        List<User> users = this.list(new QueryWrapper<User>()
                .inSql("id", "select user_id from sys_user_role where role_id = " + roleId));
        users.forEach(u -> {
            this.clearUserAuthority(u.getUsername());
        });
    }

    @Override
    public void clearUserAuthorityByMenuId(Long menuId) {
        List<User> users = userMapper.listByMenuId(menuId);
        users.forEach(u -> {
            this.clearUserAuthority(u.getUsername());
        });
    }

    @Override
    public int updateUser(UserDTO userDTO) {
        return userMapper.updateSelective(userDTO);
    }

}




